package com.zhang.authoritycenter.controller.token;

import com.zhang.authoritycenter.common.aop.OperatorLog;
import com.zhang.authoritycenter.common.model.dao.auth.OpenAccountDO;
import com.zhang.authoritycenter.common.model.request.AddOpenReqDTO;
import com.zhang.authoritycenter.manager.IOauthManager;
import com.zhang.authoritycenter.service.IOauthService;
import com.zhang.infrastructure.model.KingHoodResponseVO;
import com.zhang.infrastructure.api.model.authority.*;
import com.zhang.infrastructure.model.token.KingHoodPrincipal;
import com.zhang.infrastructure.model.token.UserDTO;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

/**
 * @author zhangFanJun
 * @date 2023-12-06 23:17
 **/
@RequiredArgsConstructor
@Slf4j
@RequestMapping("/auth")
@RestController
public class AuthController {

    private final IOauthService oauthService;
    private final IOauthManager loginManager;

    /**
     * 系统对接获取token
     *
     * @param in 系统秘钥
     * @return token信息
     * @author zfj
     * @date 2023/12/14
     */
    @PostMapping("/openToken")
    @OperatorLog
    public KingHoodResponseVO<KingHoodPrincipal<OpenAccountDO>> openToken(@RequestBody TokenReqDTO in){
        KingHoodPrincipal<OpenAccountDO> res = loginManager.openToken(in);
        return KingHoodResponseVO.success(res);
    }

    /**
     * 添加系统用户
     *
     * @param in 系统秘钥
     * @return 是否成功
     * @author zfj
     * @date 2023/12/14
     */
    @PostMapping("/addOpen")
    @OperatorLog
    public KingHoodResponseVO<Boolean> addOpen(@RequestBody AddOpenReqDTO in){

        return KingHoodResponseVO.success(loginManager.addOpen(in));
    }

    /**
     * token检查
     * */
    @PostMapping("/checkToken")
    @OperatorLog
    public KingHoodResponseVO<Boolean> validateToken(@RequestBody CheckTokenReqDTO in){

        return KingHoodResponseVO.success(oauthService.validateToken(in));
    }

    /**
     * 获取用户信息
     * 请求头携带token
     * */
    @PostMapping("/userDetail")
    @OperatorLog
    public KingHoodResponseVO<UserDTO> userDetail(){

        return KingHoodResponseVO.success(oauthService.userDetail());
    }

    /**
     * 获取用户信息和权限
     * 请求头携带token
     * */
    @PostMapping("/user")
    @OperatorLog
    public KingHoodResponseVO<UserResDTO> user(){
        return KingHoodResponseVO.success(oauthService.user());
    }

    /**
     * 获取用户信息和权限
     * 请求头不携带token
     * */
    @PostMapping("/getUser")
    @OperatorLog
    public KingHoodResponseVO<UserResDTO> getUser( @RequestBody GetUserReqDTO in){
        return KingHoodResponseVO.success(oauthService.getUser(in));
    }
}
